<?php

namespace app\system\controller;

use think\Request;
use app\system\model\SysRoleModel;
use app\system\model\SysAdminModel;
use app\system\model\SysMenuModel;

class RoleList extends MainFrame
{
    public function showList(Request $request, SysRoleModel $sysRoleModel)
    {
        $pageSize = 10;
        $roleList = $sysRoleModel->getRoleListWithPaginate($pageSize);
        $count = $roleList->total();
        $this->assign([
            "roleList" => $roleList,
            "count" => $count
        ]);
        return $this->fetch();
    }

    public function roleAdd(Request $request, SysRoleModel $sysRoleModel)
    {
        if ($request->isAjax()) {
            $data = $request->param();
            $vaResult = $this->validate($data, [
                "r_name" => "unique:sys_role",
            ], [
                "r_name" => "角色名称已存在!",
            ]);
            if (true !== $vaResult) {
                // 验证失败 输出错误信息
                return json(["code" => 2, "msg" => "{$vaResult}err:2"]);
            }

            $result = $sysRoleModel->addRoleData($data);
            if ($result != false) {
                return json(["code" => 0, "msg" => "添加成功"]);
            } else {
                return json(["code" => 1, "msg" => "失败!err:1"]);
            }
        } else {
            return $this->fetch();
        }
    }

    public function roleUpd(Request $request, SysRoleModel $sysRoleModel)
    {
        if ($request->isAjax()) {
            $data = $request->param();
            $role_id = $request->param("role_id");
            $vaResult = $this->validate($data, [
                "r_name" => "unique:sys_role",
            ], [
                "r_name" => "角色名称已存在!",
            ]);
            if (true !== $vaResult) {
                // 验证失败 输出错误信息
                return json(["code" => 2, "msg" => "{$vaResult}err:2"]);
            }

            $result = $sysRoleModel->updRoleData($data, $role_id);
            if ($result != false) {
                return json(["code" => 0, "msg" => "修改成功"]);
            } else {
                return json(["code" => 1, "msg" => "失败!err:1"]);
            }
        } else {
            $role_id = $request->param("role_id");
            $role = $sysRoleModel->getRoleById($role_id);
            $this->assign("role", $role);
            return $this->fetch();
        }
    }

    public function roleDel(Request $request, SysRoleModel $sysRoleModel, SysAdminModel $sysAdminModel)
    {
        $role_id = $request->param("role_id");
        if (!empty($role_id)) {
            $user = $sysAdminModel->where(["role_id" => $role_id])->find();
            if ($user != null) {
                return json(["code" => 2, "msg" => "角色已被用户使用，不能删除!err:2"]);
            } else {
                $delResult = $sysRoleModel->delRoleById($role_id);
                if ($delResult != false) {
                    return json(["code" => 0, "msg" => "删除成功"]);
                } else {
                    return json(["code" => 1, "msg" => "失败!err:1"]);
                }
            }
        }
    }

    public function setAuth(Request $request, SysRoleModel $sysRoleModel, SysMenuModel $sysMenuModel)
    {

        if ($request->isAjax()) {
            if ($request->isGet()) {
                $menuList = $sysMenuModel->getMenuList();
                $menuList = $this->zhLayuiArr($menuList);   //转换为layui需要形式
                $menuList = $this->buildTree($menuList);    // 构建树形结构
                return json($menuList);
            }

            if ($request->isPost()) {
                $role_id = $request->param("role_id");
                $data["auth"] = $request->param("authMenuIds");
                $result = $sysRoleModel->updRoleData($data, $role_id);
                if ($result != false) {
                    return json(["code" => 0, "msg" => "保存成功"]);
                } else {
                    return json(["code" => 1, "msg" => "失败!err:1"]);
                }
            }

        } else {
            $role_id = $request->param("role_id");
            $role = $sysRoleModel->getRoleById($role_id);
            $this->assign("role", $role);
            return $this->fetch();
        }
    }

    protected function zhLayuiArr($array)
    {
        $newArr = [];
        foreach ($array as $key => $val) {
            // 新加
            $newArr[$key]["id"] = $val["menu_id"];
            $newArr[$key]["title"] = $val["m_name"] . " id:" . $val["menu_id"];
            // 原有
            $newArr[$key]["menu_id"] = $val["menu_id"];
            $newArr[$key]["pid"] = $val["pid"];
            $newArr[$key]["m_name"] = $val["m_name"];
            $newArr[$key]["icon"] = $val["icon"];
            $newArr[$key]["mca_url"] = $val["mca_url"];
            $newArr[$key]["sort"] = $val["sort"];
        }
        // dump($newArr);die;
        return $newArr;
    }
}
